package views;

import tools.Icons;
import utils.DBUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/*ok＜（＾－＾）＞*/
public class LogInFrame extends JFrame {
    private JLabel labTitle = new JLabel("职业介绍信息管理系统");
    private Font font = new Font("TsangerYuMo W03", Font.BOLD, 34);
    private JButton btnSure = new JButton("登录");
    private JButton btnCancel = new JButton("重置");
    private JPanel panBtn = new JPanel();

    private JLabel labLoginName = new JLabel("用户名:");
    private JLabel labPWD = new JLabel("密    码:");
    private JTextField jtfLoginName;
    private JPasswordField jpfPWD;
    private JPanel panMain = new JPanel();
    ImageIcon imageIcon = new ImageIcon("src/images/manage.png");
    Icons icons = new Icons();

    public LogInFrame() {
        setTitle("登录界面");
        setIconImage(imageIcon.getImage().getScaledInstance(80, 80, Image.SCALE_DEFAULT));
        // setDefaultLookAndFeelDecorated(true);
        this.setSize(500, 350);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        init();
        add(labTitle, BorderLayout.NORTH);
        add(panBtn, BorderLayout.SOUTH);
        add(panMain, BorderLayout.CENTER);
    }

    private void init() {
        labTitle.setFont(font);
        labTitle.setHorizontalAlignment(JLabel.CENTER);
        labTitle.setForeground(Color.RED);
        // labTitle.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        labTitle.setBackground(Color.BLUE);
        jtfLoginName = new JTextField("", 10);
        jpfPWD = new JPasswordField(16);

        labLoginName.setSize(100, 40);
        labLoginName.setLocation(50, 50);
        labLoginName.setHorizontalAlignment(JLabel.RIGHT);
        labPWD.setSize(100, 40);
        labPWD.setLocation(50, 50 + 40 + 10);
        labPWD.setHorizontalAlignment(JLabel.RIGHT);

        jtfLoginName.setSize(200, 40);
        jtfLoginName.setLocation(50 + 100 + 20, 50);
        jpfPWD.setSize(200, 40);
        jpfPWD.setLocation(50 + 100 + 20, 50 + 40 + 10);
        jpfPWD.setEchoChar('*');

        btnSure.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Statement stmt = DBUtil.getStatement();
                int account = Integer.parseInt(jtfLoginName.getText().trim());
                String pwd = new String(jpfPWD.getPassword()).trim();
                String sql = "select * from admin where account= " + account + " AND password='" + pwd + "'";
                try {
                    ResultSet rs = stmt.executeQuery(sql);
                    if (rs.next()) {
                        JOptionPane.showConfirmDialog(LogInFrame.this, "登陆成功，欢迎进入!", "登陆成功", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, icons.getOkImage());
                        dispose();
                        SystemRootFrame LR = new SystemRootFrame();
                        LR.setVisible(true);
                        SystemRootFrame.ADMIN_ACCOUNT = account;
                    } else {
                        JOptionPane.showMessageDialog(null, "账号或密码错误！", "错误", JOptionPane.ERROR_MESSAGE, icons.getErrorImage());
                    }
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            }
        });
        btnCancel.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                jtfLoginName.setText("");
                jpfPWD.setText("");
            }
        });

        panBtn.add(btnSure);
        panBtn.add(btnCancel);
        panMain.setLayout(null);
        panMain.add(labLoginName);
        panMain.add(labPWD);
        panMain.add(jtfLoginName);
        panMain.add(jpfPWD);
        ImageIcon img = new ImageIcon("src/images/login.jpg");
        // 要设置的背景图片
        JLabel imgLabel = new JLabel(img);
        imgLabel.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
        // 将背景图放在标签里。
        panMain.add(imgLabel, Integer.valueOf(Integer.MIN_VALUE));

        // 将背景标签添加到jfram的LayeredPane面板里。

        // 设置背景标签的位置
        Container contain = this.getContentPane();
        ((JPanel) contain).setOpaque(false);
    }
}
